// 音频工具类
export class AudioUtils {
  // 生成简单的"叮"声音频
  static generateDingSound() {
    const audioContext = new (window.AudioContext || window.webkitAudioContext)();
    const oscillator = audioContext.createOscillator();
    const gainNode = audioContext.createGain();

    oscillator.connect(gainNode);
    gainNode.connect(audioContext.destination);

    // 设置音调为高音C (523.25 Hz)
    oscillator.frequency.value = 800;
    oscillator.type = 'sine';

    // 设置音量包络 (增大音量)
    gainNode.gain.setValueAtTime(0, audioContext.currentTime);
    gainNode.gain.linearRampToValueAtTime(0.6, audioContext.currentTime + 0.01);
    gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.3);

    // 播放音频
    oscillator.start(audioContext.currentTime);
    oscillator.stop(audioContext.currentTime + 0.3);

    return new Promise((resolve) => {
      setTimeout(resolve, 300);
    });
  }

  // 生成错误提示音
  static generateErrorSound() {
    const audioContext = new (window.AudioContext || window.webkitAudioContext)();
    const oscillator = audioContext.createOscillator();
    const gainNode = audioContext.createGain();

    oscillator.connect(gainNode);
    gainNode.connect(audioContext.destination);

    // 设置音调为低音 (200 Hz)
    oscillator.frequency.value = 200;
    oscillator.type = 'square';

    // 设置音量包络 (增大音量)
    gainNode.gain.setValueAtTime(0, audioContext.currentTime);
    gainNode.gain.linearRampToValueAtTime(0.8, audioContext.currentTime + 0.01);
    gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.5);

    // 播放音频
    oscillator.start(audioContext.currentTime);
    oscillator.stop(audioContext.currentTime + 0.5);

    return new Promise((resolve) => {
      setTimeout(resolve, 500);
    });
  }
}
